package com.yb.personelmanagement.controller.login;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yb.personelmanagement.config.jwt.JwtTokenUtil;
import com.yb.personelmanagement.domain.System.Result;
import com.yb.personelmanagement.domain.System.StatusCode;
import com.yb.personelmanagement.domain.System.User;
import com.yb.personelmanagement.service.jwt.JwtUserDetailsService;
import com.yb.personelmanagement.service.login.LoginService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api(value="用户管理功能")
@RestController
public class LoginController {
	
	@Autowired
	LoginService loginSerice;

	@Value("${jwt.header}")
	private String tokenHeader;

	@Autowired
	private AuthenticationManager authenticationManager;

	@Autowired
	private JwtTokenUtil jwtTokenUtil;

	@Autowired
	@Qualifier("jwtUserDetailsService")
	private JwtUserDetailsService userDetailsService;

	@SuppressWarnings("unused")
	@RequestMapping(value = "/redirect", method = RequestMethod.GET)
	public ModelAndView redirect(HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		List<GrantedAuthority> authorities = (List<GrantedAuthority>) SecurityContextHolder.getContext()
				.getAuthentication().getAuthorities();
		List<String> auth = new ArrayList<String>();
		for (GrantedAuthority grantedAuthority : authorities) {
			auth.add(grantedAuthority.getAuthority());
		}
		String flag = (String) request.getSession().getAttribute("flag1");
		if (flag != null) {
			return new ModelAndView("redirect:/mobileLogin");
		} else {
			return new ModelAndView("redirect:/index");
		}
	}

	@RequestMapping(value = "/mobileLogin", method = RequestMethod.GET)
	public Map<String, Object> mobile() {
		Map<String, Object> map = new HashMap<String, Object>();
		User operator = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map.put("msg", "Success");
		map.put("user", operator);
		return map;
	}

	/**
	 * 提供登录页
	 * 
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/loginIn")
	public String loginIn(ModelAndView modelAndView) {
		return "success";
	}

	/**
	 * 提供登录页
	 * 
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/login")
	public ModelAndView loginPage(ModelAndView modelAndView) {
		modelAndView.setViewName("project/login");
		return modelAndView;
	}

	@ApiOperation(value = "用户登录接口返回结果包含token")
	@PostMapping("/login")
    @ApiImplicitParam(paramType="query", name = "userName", value = "用户名", required = true, dataType = "String")
   
	public Result login(String userName,String passWord) {
		
		Result checkResult=loginSerice.checkUser(userName, passWord);
		if(checkResult!=null && checkResult.isFlag()) {
			//登陆成功
			
			User user=(User)checkResult.getObject();
			//生成token
			String token=jwtTokenUtil.generateToken(user);
			user.setToken(token);
			return new Result(true,user,"登陆成功",StatusCode.SUCCESS);
							
			
		}else {
			
			return checkResult;
		}
		
	}

}
